package com.smart.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.LoginUser;
import com.smart.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.resp.UserResp;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author lyj
 * @since 2023-04-19
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select count(1)\n" +
            "from user\n" +
            "where work_num like concat(#{prefix},'%')")
    int getCountByPrefix(String prefix);

    @Select("select u.*\n" +
            "from user u,user_role ur,role r ${ew.customSqlSegment}")
    List<User> queryByRoleIdAndKeyword(@Param("ew") QueryWrapper<User> wrapper);

    @Select("select u.*,r.name roleName\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "where u.del_flag=0\n" +
            "and r.del_flag=0\n" +
            "and u.id=#{id}")
    UserResp queryById(Serializable id);

    @Select("select u.*,\n" +
            "r.name roleName,\n" +
            "r.base_salary baseSalary,\n" +
            "d.id deptId,\n" +
            "d.name deptName\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id \n" +
            "and r.del_flag=0\n" +
            "left join department d\n" +
            "on r.department_id=d.id \n" +
            "and d.del_flag=0\n" +
            "where u.del_flag=0\n" +
            "and u.id=#{id}")
    LoginUser queryUserInfoById(Long id);

    @Select("select u.*,\n" +
            "r.name roleName,\n" +
            "ifnull(r.base_salary,4500) baseAmount\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "and r.del_flag=0\n" +
            "where u.del_flag=0 and u.status=0")
    List<UserResp> queryAll();

    @Select("select u.*,r.name roleName\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "and r.del_flag=0\n" +
            "left join department d\n" +
            "on r.department_id=d.id\n" +
            "and d.del_flag=0\n" +
            "where u.del_flag=0 and u.status=0\n" +
            "and d.id=#{departmentId}")
    List<UserResp> queryByDeptId(Long departmentId);

    @Select("select u.*,\n" +
            "r.name roleName,\n" +
            "d.name departmentName,\n" +
            "r.base_salary baseAmount,\n" +
            "u1.username createName,\n" +
            "u2.username updateName\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "and r.del_flag=0\n" +
            "left join department d\n" +
            "on r.department_id=d.id\n" +
            "and d.del_flag=0\n" +
            "left join user u1\n" +
            "on u.create_by=u1.id\n" +
            "left join user u2\n" +
            "on u.update_by=u2.id\n" +
            "where u.id=#{userId}")
    UserResp getUserInfoById(Long userId);

    @Select("select u.*,\n" +
            "r.name roleName,\n" +
            "d.name departmentName,\n" +
            "r.base_salary baseAmount\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "and r.del_flag=0\n" +
            "left join department d\n" +
            "on r.department_id=d.id\n" +
            "and d.del_flag=0 ${ew.customSqlSegment}")
    Page<UserResp> queryPageByCondition(Page<UserResp> userPage, @Param("ew") QueryWrapper<UserResp> wrapper);

    @Select("select u.*,r.name roleName\n" +
            "from user u\n" +
            "left join role r\n" +
            "on u.role_id=r.id\n" +
            "and r.del_flag=0\n" +
            "left join department d\n" +
            "on r.department_id=d.id\n" +
            "and d.del_flag=0\n" +
            "where u.del_flag=0 and u.status=0\n" +
            "and d.id=#{departmentId}")
    Page<UserResp> queryPageByDeptId(@Param("departmentId") Long departmentId, Page<UserResp> userPage);
}
